package cn.picclife.mwx.common.core.util;

import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;


public class CustomerStringUtils {

    public static boolean isChineseChar(String str) {
        boolean temp = false;
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        if (m.find()) {
            temp = true;
        }
        return temp;
    }

    /**
     * 根据身份证   字符串   返回 相关信息
     * @param idCard [0]性别  M:male  F:female	  [1]出生日期  yyyy-MM-dd
     * @return
     */
    public static String[] formatIDCard(String idCard) {
        String[] returnStr = new String[2];
        char[] str = idCard.toCharArray();
        // 倒数第二位 判断性别 男单女双
        if (str[16] % 2 == 0) {
            returnStr[0] = "F";
        } else {
            returnStr[0] = "M";
        }
        // 拼接 出生年月日
        returnStr[1] = str[6] + "" + str[7] + str[8] + str[9] + "-" + str[10] + str[11] + "-"
                + str[12] + str[13];
        return returnStr;
    }

    /**
     * 按长度 生成  随机 数字 组合
     * @param len
     * @return
     */
/*    public static String generateIntNum(int len) {
        Random r = new Random();
        String str = "";
        for (int i = 0; i < len; i++) {
            str += r.nextInt(10);
        }
        return str;
    }*/

    /**
     * 按长度 生成  随机 字符 组合
     * @param len
     * @return
     */
/*    public static String generateString(int len) {
        String source = "abcdefghijklmnopqrstuvwxyz1234567890";
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < len; i++) {
            sb.append(source.charAt(random.nextInt(source.length())));
        }
        return sb.toString();
    }*/

    public static String replaceBlank(String str) {
        String dest = "";
        if (str != null) {
            Pattern p = Pattern.compile("\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }

    public static String processStrings(String str) {
        if (str != null) {
            str = StringUtils.replace(str, "&", "&amp;");
            str = StringUtils.replace(str, " ", "&nbsp;");
            str = StringUtils.replace(str, "<", "&lt;");
            str = StringUtils.replace(str, ">", "&gt;");
            str = StringUtils.replace(str, "\r\n", "<br>");
            str = StringUtils.replace(str, "\"", "&quot;");

            return (str);
        } else
            return (str);
    }

    /**
     * 
    * @Title: toChinese 
    * @Description: 阿拉伯数字转汉字
    * @param @param string
    * @param @return    设定文件 
    * @return String    返回类型 
    * @date 2017年12月26日 下午4:21:05
    * @throws
     */
    public static String toChinese(String string) {
        String[] s1 = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
        String[] s2 = {"十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千"};

        String result = "";

        int n = string.length();
        for (int i = 0; i < n; i++) {

            int num = string.charAt(i) - '0';

            if (i != n - 1 && num != 0) {
                result += s1[num] + s2[n - 2 - i];
            } else {
                result += s1[num];
            }
        }
        return result;

    }
}
